
//
//  GouWuCheViewController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/7/27.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "GouWuCheViewController.h"
#import "GouWuCheView.h"

#import "TuiHuanTableViewCell.h"
#import "GouWuCheDetailViewController.h"

@interface GouWuCheViewController ()<gouWuCheDelegate,UITableViewDelegate, UITableViewDataSource>
{
    BOOL flag;//cell的选中与否
    BOOL sectionFlag;//分区的选中与否
    
    int countNum;//要结算的数量
    
}
@property (nonatomic, strong)GouWuCheView *gouwucheView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, strong)NSMutableArray *cellBtnArray;

@end

@implementation GouWuCheViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBar.barTintColor = [ UIColor whiteColor];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    _cellBtnArray = [NSMutableArray array];
    flag = NO;
    sectionFlag = NO;
    countNum = 0;
    [self congureNaBar];
    [self configureViewC];
}


//配置导航栏
- (void)congureNaBar {
    UIButton *informationCardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [informationCardBtn addTarget:self action:@selector(narBaredit:) forControlEvents:UIControlEventTouchUpInside];
    [informationCardBtn setTitle:@"编辑" forState:(UIControlStateNormal)];
     [informationCardBtn setTitle:@"完成" forState:(UIControlStateSelected)];
    informationCardBtn.frame = CGRectMake(0, 0, 50, 30);
    [informationCardBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    UIBarButtonItem *informationCardItem = [[UIBarButtonItem alloc] initWithCustomView:informationCardBtn];
    
    
    UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpaceBarButtonItem.width = 10;
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingBtn addTarget:self action:@selector(narBarMessage:) forControlEvents:UIControlEventTouchUpInside];
    [settingBtn setImage:[UIImage imageNamed:@"fenlei_xiaoxi"] forState:UIControlStateNormal];
    //[settingBtn sizeToFit];
    settingBtn.frame = CGRectMake(0, -5, 30, 30);
    UIBarButtonItem *settingBtnItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    
    
    
    self.navigationItem.rightBarButtonItems  = @[settingBtnItem,fixedSpaceBarButtonItem,informationCardItem];
    

}

#pragma mark 导航蓝编辑按钮的点击事件
- (void)narBaredit :(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        
        
        //部分按钮隐藏，删除按钮出现
        [_gouwucheView hiddenOrPlayFlage:YES];
    }else {
        //部分按钮出现，删除按钮隐藏
        [_gouwucheView hiddenOrPlayFlage:NO];
    }
}
#pragma mark 导航栏消息按钮的点击事件
- (void)narBarMessage:(UIButton *)sender {
 
    
  
}
//配置控制器
- (void)configureViewC {
    _gouwucheView = [[GouWuCheView alloc] initWithFrame:CGRectMake(0, kScreen_Height-44-50, kScreen_Width, 50)];
    _gouwucheView.delegate = self;
    
    _gouwucheView.countNumStr = @"结算(0)";
    _gouwucheView.postMoneyStr = @"20";
    _gouwucheView.countMoneyStr = @"¥14678.0";
    [self.view addSubview:_gouwucheView];
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-50-44-64) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120;
    self.tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = kSpNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"TuiHuanTableViewCell" bundle:nil] forCellReuseIdentifier:@"TuiHuanTableViewCell"];
    
    [self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    else {
    return 2;    
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TuiHuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TuiHuanTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.selectBtn.selected = flag;
    
        cell.selectBtn.tag = indexPath.section *200+indexPath.row;
    
        [cell.selectBtn addTarget:self action:@selector(handleSeleOneAction:) forControlEvents:(UIControlEventTouchUpInside)];
        cell.NumberBackView.hidden = !flag;
        return cell;
}

//进入商品详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    GouWuCheDetailViewController *vc = [[GouWuCheDetailViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 50;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;
    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 10, kScreen_Width, 39)];
    bgV.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:bgV];
    
    UIButton *allBtn = [BasePublicCreatUI createBtnFrame:CGRectMake(10, 10, 20, 20) title:nil bgImage:nil selectBgImage:@"gouwuche_quan2" image:@"gouwuche_quan" target:self action:@selector(allBtnSelect2:)];
    allBtn.selected = sectionFlag;
    allBtn.tag = section+100;
    [bgV addSubview:allBtn];
    
    UILabel *titleLable = [BasePublicCreatUI createLabelFrame:CGRectMake(CGRectGetMaxX(allBtn.frame)+10, 0, kScreenWidth-50, 40) title:@"胡来超市" textColor:[UIColor blackColor] textAlignment:NSTextAlignmentLeft font:[UIFont systemFontOfSize:15]];
    [bgV addSubview:titleLable];
    
    
    return headerView;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;
    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 1, kScreen_Width, 39)];
    bgV.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:bgV];
    UILabel *titLB = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 39)];
    titLB.text = @"重量2kg,运费20元";
    titLB.textColor = [UIColor lightGrayColor];
    titLB.font = kFont15;
    [bgV addSubview:titLB];
    
    
    return headerView;
}


//分区按钮
-(void)allBtnSelect2:(UIButton *)sender{
    sender.selected = !sender.selected;

    NSInteger section =sender.tag -100;
  
    if (sender.selected) {
        flag = YES;
        sectionFlag = YES;
        countNum =  countNum+2;
      
        
    }else {
        if (_cellBtnArray) {
            [_cellBtnArray removeAllObjects];
        }
        flag = NO;
        sectionFlag = NO;
       
         countNum = countNum -2;
    }
   
    
    /*
    //底部视图赋值
    _gouwucheView.countMoneyStr = @"¥1220.0";
    [_gouwucheView.counBtn setTitle:[NSString stringWithFormat:@"结算(%d)",countNum] forState:(UIControlStateNormal)];;
     */
    //刷新选中section
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:section];
    [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    
    
}
#pragma mark 底部视图全选按钮的代理事件
- (void)selectAllAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        flag = YES;
    }else {
        flag = NO;
       
    }
    [self.tableView reloadData];
    
}

//row中的按钮
- (void)handleSeleOneAction:(UIButton *)ssender {
    
    //ssender.selected = !ssender.selected;
    
    
    
    
    UITableViewCell *cell = (UITableViewCell *)[[ssender superview] superview];
    NSIndexPath *indepath = [_tableView indexPathForCell:cell];
  
    /*获取改分区下的所有cell
    NSInteger sections = indepath.section;
    NSMutableArray *cells = [[NSMutableArray alloc]  init];
     NSInteger rows =  [_tableView numberOfRowsInSection:sections];
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:indepath.section];
        [cells addObject:[_tableView cellForRowAtIndexPath:indexPath]];
    }*/
 
    
    
    
    
    if (ssender.selected) {
        NSLog(@"111");
         countNum = countNum +1;
        [_cellBtnArray addObject:[NSString stringWithFormat:@"%ld",ssender.tag]];
      
        
        //判断数组的个数是不是等于改分区所有的cell的个数，如果＝＝就把改分区的按钮重置为yes
        if (_cellBtnArray.count == 3) {
            UIButton *btn = (UIButton *)[self.view viewWithTag:indepath.section+100];
            btn.selected = YES;
        }
        
    }
    else {
        //如果有一个cell未选中则改分区的的按钮重置为未选中
        UIButton *btn = (UIButton *)[self.view viewWithTag:indepath.section+100];
        btn.selected = NO;
        countNum = countNum -1;
        [_cellBtnArray removeObject:[NSString stringWithFormat:@"%ld",ssender.tag]];
    }
    
    /*
    _gouwucheView.countMoneyStr = @"20";
   [_gouwucheView.counBtn setTitle:[NSString stringWithFormat:@"结算(%d)",countNum] forState:(UIControlStateNormal)];;
     */
    
}
#pragma mark 购物车视图的代理事件

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
